<template>
  <el-dialog :title="$t('financeModule.repayList.tips.title')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false" width="80%">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="120px" class="demo-ruleForm">
      <!--结算方式-->
      <el-form-item :label="$t('financeModule.repayList.table.repayMethod')" prop="isDone">
        <el-select v-model="formValidate.isDone">
          <el-option
            v-for="item in repayMethod"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!--还款渠道-->
      <el-form-item v-if="!formValidate.isDone" :label="$t('financeModule.repayList.table.repayChannel')" prop="repayType">
        <el-select v-model="formValidate.repayType" :placeholder="$t('financeModule.repayList.tips.repayChannel')">
          <el-option
            v-for="item in $t('financeModule.repayList.repayType')"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!--应还金额-->
      <el-form-item v-if="formValidate.isDone === true" :label="$t('financeModule.repayList.table.repayShuold')">
        <el-input v-model="formValidate.currentRepay" readonly />
      </el-form-item>
      <!--已还金额-->
      <el-form-item v-if="formValidate.isDone === true" :label="$t('financeModule.repayList.table.amountRepaid')">
        <el-input v-model="formValidate.realRepayMoney" readonly />
      </el-form-item>
      <!--未结金额-->
      <el-form-item v-if="formValidate.isDone === true" :label="$t('financeModule.repayList.table.openAmount')">
        <el-input v-model="formValidate.repayMoney" readonly />
      </el-form-item>
      <!--还款金额-->
      <el-form-item v-if="!formValidate.isDone" :label="$t('financeModule.repayList.table.repayTotal')" prop="repayMoney">
        <el-input-number
          v-model="formValidate.repayMoney"
          :controls="false"
          :max="10000000000000"
          :placeholder="$t('financeModule.repayList.tips.repayTotal')"
          style="width: 100%"
        />
      </el-form-item>
      <!--流水号-->
      <el-form-item v-if="!formValidate.isDone" :label="$t('financeModule.repayList.table.serialNumber')" prop="serialNumber">
        <el-input v-model="formValidate.serialNumber" clearable :placeholder="$t('financeModule.repayList.tips.serialNumber')" :maxlength="100" />
      </el-form-item>
      <el-form-item v-if="formValidate.isDone === 1" :label="$t('financeModule.repayList.table.serialNumber')" prop="serialNumber">
        <el-select v-model="formValidate.serialNumber" :placeholder="$t('financeModule.repayList.tips.serialNumber')" style="width: 100%">
          <el-option
            v-for="(item, index) in serialNumberData"
            :key="index"
            :label="item.serialNumber"
            :value="item.serialNumber"
          />
        </el-select>
      </el-form-item>
      <!--金额-->
      <el-form-item v-if="formValidate.isDone === 1" :label="$t('financeModule.repayList.table.mount')" prop="money">
        <el-input-number
          v-model="formValidate.money"
          :controls="false"
          :max="10000000000000"
          :placeholder="$t('financeModule.repayList.tips.mount')"
          style="width: 100%"
        />
      </el-form-item>
      <!--还款时间-->
      <el-form-item v-if="!formValidate.isDone" :label="$t('financeModule.repayList.table.repayData')" prop="repaymentDate">
        <el-date-picker
          v-model="formValidate.repaymentDate"
          type="datetime"
          :placeholder="$t('financeModule.repayList.table.repayData')"
        />
      </el-form-item>
      <!--备注-->
      <el-form-item :label="$t('financeModule.repayList.table.remark')" :prop="formValidate.isDone === 1 ? 'description' : ''">
        <el-input
          v-model="formValidate.description"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('financeModule.repayList.tips.remark')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { financeRepayment } from '@/api/finance'
import { formatDate } from '@/utils/method'
export default {
  name: 'RepayListDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    repayMethod: {
      type: Array,
      default: function() {
        return []
      }
    },
    serialNumberData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        currentRepay: null,
        description: null,
        isCancelRepayment: null,
        isDone: null,
        orderId: null,
        repayMoney: null,
        repayType: null,
        repaymentDate: null,
        serialNumber: null,
        loanType: null,
        money: null,
        repaymentPlanId: null,
        manualRepaymentId: null,
        realRepayMoney: null,
        userId: null,
        currentPeriod: null,
        totalPeriod: null,
        operator: 1,
        operationResult: 0
      },
      rules: {
        isDone: [{ required: true, message: this.$t('financeModule.repayList.table.repayMethod') + this.$t('common.null'), trigger: 'blur' }],
        repayType: [{ required: true, message: this.$t('financeModule.repayList.table.repayChannel') + this.$t('common.null'), trigger: 'blur' }],
        repayMoney: [{ required: true, message: this.$t('financeModule.repayList.table.repayTotal') + this.$t('common.null'), trigger: 'blur' }],
        serialNumber: [{ required: true, message: this.$t('financeModule.repayList.table.serialNumber') + this.$t('common.null'), trigger: 'blur' }],
        money: [{ required: true, message: this.$t('financeModule.repayList.table.mount') + this.$t('common.null'), trigger: 'blur' }],
        repaymentDate: [{ required: true, message: this.$t('financeModule.repayList.table.repayData') + this.$t('common.null'), trigger: 'blur' }],
        description: [{ required: true, message: this.$t('financeModule.repayList.table.remark') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.formValidate))
          if (!data.isDone) {
            data.isCancelRepayment = false
            data.repaymentDate = formatDate(new Date(data.repaymentDate), 'yyyy-MM-dd hh:mm:ss')
          } else if (data.isDone === true) {
            data.isCancelRepayment = false
            data.repayType = null
            data.repaymentDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
            data.serialNumber = null
          } else {
            data.isDone = false
            data.isCancelRepayment = true
            data.repayType = null
            data.repaymentDate = null
          }
          this.submitLoading = true
          financeRepayment(this.formValidate)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
